﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using DVO;

public partial class TestDocument : System.Web.UI.Page
{
    public int _WId = 0;
    public int _Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.User.Identity.Name == "")
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            if (!String.IsNullOrEmpty(Request.QueryString["WId"]))
            {
                _WId = Convert.ToInt32(Request.QueryString["WId"].ToString());
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CId"]))
            {
                _Id = Convert.ToInt32(Request.QueryString["CId"].ToString());
            }
            if (IsPostBack == false)
            {
                if (_WId > 0)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(HttpContext.Current.Session["r"].ToString()))
                        {
                            if (HttpContext.Current.Session["r"].ToString() == "3")
                            {
                                btnsubmit.Text = "Phê Duyệt";
                            }
                            else
                            {
                                LoadEmail();
                            }
                        }
                        else
                        {
                            Response.Redirect("Login.aspx");
                        }
                    }
                    catch (Exception ex)
                    {
                        Common.WriteLog(ex.Message, ex.StackTrace);
                        Response.Redirect("Login.aspx");
                    }
                    LoadData(_WId);
                   
                }
            }
        }
        

    }
    #region "LoadData"
    private void LoadData(int workId)
    {
        BLL_Document ObjBLL = new BLL_Document();
        ins_Document_SelectByIdResult ObjResult = new ins_Document_SelectByIdResult();
        ObjResult = ObjBLL.Document_SelectById(workId);
        txtDocumentName.Text = ObjResult.DocumentName;
        txtUserName.Text = ObjResult.Author;
        txtWorkName.Text = ObjResult.WorkName;
        txtNotes.Text = ObjResult.Notes;
        Linkdocument.Visible = true;
        Linkdocument.HRef = ObjResult.LinkDocument;
        hdfdocumentId.Value = ObjResult.Id.ToString();
        hdfworkid.Value = workId.ToString();
        hdfemail.Value = ObjResult.Email;
    }

    private void LoadEmail()
    {
        int Id=0;
        string email = "";
        if(!string.IsNullOrEmpty(Request.QueryString["WId"]))
        {
            Id = Convert.ToInt32(Request.QueryString["WId"].ToString());
        }
        BLL_Approve ObjBll = new BLL_Approve();
        BLL_Member Objmember = new BLL_Member();
        List<ins_Member_SelectLevelResult> Objlistmember = new List<ins_Member_SelectLevelResult>();
        List<ins_Approve_SelectUserResult> Objlist = new List<ins_Approve_SelectUserResult>();
         Objlist = ObjBll.Approve_SelectUser(Id).ToList();
        for (int i = 0; i < Objlist.Count; i++)
        {
            email += Objlist[i].UserName + ";";
        }
        email = email.Substring(0, email.Length - 1);
        try
        {
            Objlistmember = Objmember.Level_Select(3);
            ckbemail.DataSource = Objlistmember;
            ckbemail.DataTextField = "UserName";
            ckbemail.DataValueField = "Email";
            ckbemail.DataBind();    
            String[] adrr = email.Split(';');
            foreach (ListItem item in ckbemail.Items)
            {
                if(adrr.Contains(item.Text))
                {
                    item.Selected = true;
                }
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog(ex.Message, ex.StackTrace);
        }
    }
    #endregion
    


    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        string email = "";
        int x = 0;
        foreach (ListItem item in ckbemail.Items)
        {
            if (item.Selected == true)
            {
                x = x + 1;
                email += item.Value + ";";
            }
        }
        //String [] adrr=email.Split(';');

        if (x == 0)
        {
            lbmessage.Text = "Chưa chọn người quản lý để gửi mail!";
            return;
        }
        email = email.Substring(0, email.Length - 1);
        string author="";
        string level = "";
        if (!String.IsNullOrEmpty(Page.User.Identity.Name))
        {
            author = Page.User.Identity.Name;
        }
        BLL_Document ObjBLL = new BLL_Document();
        //ins_Document_SelectByIdResult ObjResult = new ins_Document_SelectByIdResult();
        //ObjResult = ObjBLL.Document_SelectById(_WId);
        //_Id = ObjResult.Id;
        int status = 0;
        try
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Session["r"].ToString()))
            {
                level = HttpContext.Current.Session["r"].ToString();
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog(ex.Message, ex.StackTrace);
            Response.Redirect("Login.aspx");
        }
        if (level == "2")
        {
            status = 2;
        }
        else
            status = 1;
        if (ObjBLL.Update_Document(_WId, status) > 0)
        {
            BLL_Approve ObjApprove = new BLL_Approve();
            int y = 0;
            foreach (ListItem item in ckbemail.Items)
            {
                if (item.Selected == true)
                {
                    if (ObjApprove.Approve_Insert(_WId, item.Text, status) > 0)
                    {
                        y = y + 1;
                    }
                }
            }
            if (y > 0)
            {
                if (ckbAttach.Checked == true)
                {
                    if (EmailHelper.SendNewMessage(Server.MapPath("~/Resources/Template"), email, "1", _WId) == true)
                    {
                        lbmessage.Text = "Gửi tài liệu thành công";
                        return;
                    }
                    else
                    {
                        lbmessage.Text = "Gửi tài liệu thành công";
                        return;
                    }
                }
                else
                {
                    if (EmailHelper.SendNewMessage(Server.MapPath("~/Resources/Template"), email, "", _WId) == true)
                    {
                        lbmessage.Text = "Gửi tài liệu thành công";
                        return;
                    }
                    else
                    {
                        lbmessage.Text = "Gửi tài liệu thành công";
                        return;
                    }
                }
            }
        }

    }
    protected void btnfeedback_Click(object sender, EventArgs e)
    {
        if (txtNotes.Text == "")
        {
            lbmessage.Text = "Chưa điền ghi chú!";
            return;
        }
        //if (txtUserMail.Text == "")
        //{
        //    lbmessage.Text = "Chưa điền mail người nhận";
        //    return;
        //}
        BLL_Working ObjBLL = new BLL_Working();
        string comment = txtComment.Text.Trim();
        string author = "";
        if (!String.IsNullOrEmpty(Page.User.Identity.Name))
        {
            author = Page.User.Identity.Name;
        }
        int documentid = Convert.ToInt32(hdfdocumentId.Value);
        int workid = Convert.ToInt32(hdfworkid.Value);
        int status = 3;
        int id = Convert.ToInt32(hdfcommentId.Value);
        if (ObjBLL.Insert_Comment(documentid, comment, author, status, id, workid,0) > 0)
        {
            if (ckbAttach.Checked == true)
            {
                if (EmailHelper.Sendmailfeedback(Server.MapPath("~/Resources/Template"), hdfemail.Value, "1", documentid,txtComment.Text.Trim()) == true)
                {
                    lbmessage.Text = "Gửi phản hồi thành công";
                }
                else
                {
                    lbmessage.Text = "Gửi phản hồi thành công";
                    return;
                }
            }
            else
            {
                if (EmailHelper.Sendmailfeedback(Server.MapPath("~/Resources/Template"), hdfemail.Value, "", documentid, txtComment.Text.Trim()) == true)
                {
                    lbmessage.Text = "Gửi phản hồi thành công";
                }
                else
                {
                    lbmessage.Text = "Gửi phản hồi thành công";
                    return;
                }
            }
        }
        else
        {
            lbmessage.Text = "Giử phản hồi thất bại";
        }
    }
}